﻿using System;
using System.IO;
using System.Numerics;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class BigIntegerProcessor : GenericDataProcessor<BigInteger>
        {
            public override bool IsSystem
            {
                get
                {
                    return true;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "System.Numerics.BigInteger";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "bigint",
                    "biginteger",
                    "system.numerics.biginteger"
                };
            }

            public override BigInteger Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return BigInteger.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                binaryWriter.Write(Parse(value).ToByteArray());
            }
        }
        private sealed class BigIntegerArrayProcessor : GenericDataArrayProcessor<BigInteger>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "System.Numerics.BigInteger[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "bigint[]",
                    "biginteger[]",
                    "system.numerics.biginteger[]"
                };
            }

            public override BigInteger ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return BigInteger.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    binaryWriter.Write(array[i].ToByteArray());
                }
            }
        }
        private sealed class BigIntegerListProcessor : GenericDataListProcessor<BigInteger>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<System.Numerics.BigInteger>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<bigint>",
                    "list<biginteger>",
                    "system.collections.generic.list<bigint>",
                    "system.collections.generic.list<biginteger>",
                };
            }

            public override BigInteger ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return BigInteger.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    binaryWriter.Write(array[i].ToByteArray());
                }
            }
        }
        private sealed class BigIntegerDictionaryProcessor : GenericDataDictionaryProcessor<BigInteger>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,System.Numerics.BigInteger>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,bigint>",
                    "dictionary<string,biginteger>",
                };
            }

            public override BigInteger ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return BigInteger.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    binaryWriter.Write(item.Value.ToByteArray());
                }
            }
        }
    }
}
